<?php
define("baidukey","3AGfeNXGhgzw4cjcGeCY8ivz");

class BaiduMapAction extends Action {

    
	/**
	 * 根据地址名称获取经纬度
	 * @param unknown $address
	 */
    public function geocoder($address){
    	if (!isset($address)) {
    		Output::instance()->resultCode(code_success);
    		return;
    	}
    	$url = "http://api.map.baidu.com/geocoder?address=".$address."&output=json&key=".baidukey  ;
    	
    		$ch = curl_init($url) ;
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
    		curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
    		$result = curl_exec($ch) ;
    		
    		curl_close($ch);
    		
    		$bresult = json_decode($result);
    	
	    	if (($bresult->status) === 'OK') {
	    		$result = new stdClass();
	    		$result->lng = $bresult->result->location->lng;
	    		$result->lat = $bresult->result->location->lat;
	    		Output::instance()->object($result);
			} else {
				Output::instance()->resultCode(code_success);
			}
    }
    
    /**
     * 根据经纬度获取地址
     * @param unknown $lng
     * @param unknown $lat
     */
    public function dgeocoder($lng,$lat){
    	if (!isset($lng) ||!isset($lat)) {
    		Output::instance()->resultCode(code_success);
    		return;
    	}
    	$url = "http://api.map.baidu.com/geocoder?output=json&location=".$lat.",%20".$lng."&key=".baidukey;
//     	$url = "http://api.map.baidu.com/geocoder/v2/?location=".$lat.",".$lng."&pois=0&output=json&ak=".baidukey  ;
    	Log::write($url);
    		$ch = curl_init($url) ;
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
    		curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
    		$result = curl_exec($ch) ;
    		
    		curl_close($ch);
    		
    		$bresult = json_decode($result);
    	
	    	if (($bresult->status) === 'OK') {
	    		$result = new stdClass();
	    		$result->formatted_address = $bresult->result->formatted_address;
	    		$result->business = $bresult->result->business;
	    		
	    		Output::instance()->object($result);
			} else {
				Output::instance()->resultCode(code_success);
			}
    }
}